package com.whl.sm.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.system.Community;
import com.whl.sm.model.system.OrgUser;
import com.whl.sm.service.system.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-09
 */
@RestController
@RequestMapping("/community")
public class CommunityController extends BaseController {

    @Autowired
    CommunityService communityService;

    @RequestMapping("/list")
    public Result getComList(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("com_name");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("update_time");
        Page<Object> userPage = communityService.page(getPage(),queryWrapper);
        return Result.ok(userPage);
    }

    @RequestMapping("/info/{id}")
    public Result commInfo(@PathVariable("id") String id){
        Community community = communityService.getById(id);
        return Result.ok(community);
    }


    @RequestMapping("/update")
    public Result updateUser(@RequestBody Community community){
        communityService.update(community);
        return Result.ok();
    }

    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            communityService.update(new UpdateWrapper<Community>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }



}
